#define _CRT_SECURE_NO_WARNINGS 1

#include<functional>
namespace xrw
{
	template<class T>
	class shared_ptr
	{
	public:
		shared_ptr(T* ptr)
			:_ptr(ptr)
			,_count(new int(1))
		{

		}

		template<class D>
		shared_ptr(T* ptr,D del)
			:_ptr(ptr)
			, _count(new int(1))
			,_del(del)
		{}

		~shared_ptr()
		{
			if (--(*_count) == 0)
			{
				_del(_ptr);
				delete _count;
			}
		}

		shared_ptr(const shared_ptr<T>& sp)
			:_ptr(sp._ptr)
			, _count(sp._count)
			,_del(sp._del)
		{
			++(*_count);
		}


		shared_ptr& operator=(const shared_ptr<T>& sp)
		{
			if (--(*_count) == 0)
			{
				_del(_ptr);
				delete _count;
			}

			_ptr = sp._ptr;
			_count = sp._count;
			++(*_count);
		}

		T* get() const
		{
			return _ptr;
		}

		T& operator*()
		{
			return *_ptr;
		}

		T* operator->()
		{
			return _ptr;
		}

	private:
		T* _ptr;
		int* _count;
		function <void(T*)> _del = [](T* ptr) {delete ptr; };
	};

	template<class T>
	class weak_ptr
	{
	public:
		weak_ptr()
			:_ptr(nullptr)
		{

		}

		weak_ptr(const shared_ptr<T>& sp)
			:_ptr(sp.get())
		{

		}

		weak_ptr& operator= (const shared_ptr<T>& sp)
		{
			_ptr = sp.get();
			return *this;
		}

		T& operator*()
		{
			return *_ptr;
		}

		T* operator->()
		{
			return _ptr;
		}
	private:
		T* _ptr;
	};
}